package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.activity.TwoDatePickActivity;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditOrderWorkpartAdapter;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditOrderWorkpartAdapter1;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderEditPresenter;
import com.cloudsigns.factorymanager2.project.iview.IProductDetailView;
import com.cloudsigns.factorymanager2.project.presenter.ProductDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceEditOrderActivity1 extends BaseActivity implements IProductDetailView {
    private String TAG = ProduceEditOrderActivity1.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_product_name;
    private TextView tv_order_information;
    private ImageView img_production_cycle;
    private RelativeLayout rl_production_cycle;
    private TextView tv_production_cycle;
    private ImageView img_add_workpart;
    private View v_1;
    private ListViewForScrollView lv_add_workpart;

    private Produce_ProductBean produce_productBean;

    private String beginDate, completeDate;

    private ProduceEditOrderWorkpartAdapter1 adapter;
    private List<WorkPartBean> workPartBeanList;

    private ProductDetailPresenter productDetailPresenter;

    private int selectedPosition = -1;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_order1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_product_name = findViewById(R.id.tv_product_name);
        tv_order_information = findViewById(R.id.tv_order_information);
        img_production_cycle = findViewById(R.id.img_production_cycle);
        rl_production_cycle = findViewById(R.id.rl_production_cycle);
        tv_production_cycle = findViewById(R.id.tv_production_cycle);
        img_add_workpart = findViewById(R.id.img_add_workpart);
        v_1 = findViewById(R.id.v_1);
        lv_add_workpart = findViewById(R.id.lv_add_workpart);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_order));

        Intent intent = getIntent();
        if (null != intent) {
            produce_productBean = (Produce_ProductBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        productDetailPresenter = new ProductDetailPresenter(this);
        productDetailPresenter.getProductDetail();

        tv_product_name.setText(produce_productBean.getProductName());

        workPartBeanList = produce_productBean.getWorkPartList();

        adapter = new ProduceEditOrderWorkpartAdapter1(this);
        adapter.setData(workPartBeanList);
        lv_add_workpart.setAdapter(adapter);

        lv_add_workpart.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                selectedPosition = position;
                if (rl_production_cycle.getVisibility() == View.GONE) {
                    showToast(getResources().getString(R.string.edit_produce_cycle));
                } else {
                    WorkPartBean wp = workPartBeanList.get(position);
                    Intent intent = new Intent(ProduceEditOrderActivity1.this, ProduceWorkpartInformationActivity1.class);
                    wp.setProductId(produce_productBean.getProductId());
                    wp.setOrderId(produce_productBean.getOrderId());
                    intent.putExtra(ConstantUtil.KEY_1, wp);
                    intent.putExtra(ConstantUtil.KEY_2,position);
                    startActivityForResult(intent, ConstantUtil.START_ACTIVITY_KEY_3);
                }
            }
        });
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        tv_order_information.setOnClickListener(this);
        img_production_cycle.setOnClickListener(this);
        img_add_workpart.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 工单信息
            case R.id.tv_order_information:
                mIntent = new Intent(ProduceEditOrderActivity1.this, ProduceReceiveOrderItemDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_2, "ProduceEditOrderActivity");
                mIntent.putExtra("orderId", produce_productBean.getOrderId());
                mIntent.putExtra("productId", produce_productBean.getProductId());
                startActivity(mIntent);
                break;
            // 编辑生产周期
            case R.id.img_production_cycle:
                mIntent = new Intent(ProduceEditOrderActivity1.this, TwoDatePickActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
                break;
            // 添加工序
            case R.id.img_add_workpart:
                mIntent = new Intent(ProduceEditOrderActivity1.this, ProduceAddWorkpartActivity1.class);
                for (WorkPartBean workPartBean : workPartBeanList) {
                    if (workPartBean.isLastWorkPart()) {
                        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                        break;
                    }
                }
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // 添加工序
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    v_1.setVisibility(View.VISIBLE);

                    ArrayList<WorkPartBean> tmpList = (ArrayList<WorkPartBean>) data.getSerializableExtra(ConstantUtil.KEY_2);
                    for (WorkPartBean workPartBean : tmpList) {
                        workPartBeanList.add(workPartBean);
                    }


                    WorkPartBean lastWorkPartBean = null;
                    for (WorkPartBean workPartBean : workPartBeanList) {
                        if (workPartBean.isLastWorkPart()) {
                            lastWorkPartBean = workPartBean;
                            break;
                        }
                    }
                    if (lastWorkPartBean != null) {
                        for (WorkPartBean workPartBean : workPartBeanList) {
                            if (workPartBean.getWorkPartName().equals(lastWorkPartBean.getWorkPartName())) {
                                workPartBean.setLastWorkPart(true);
                            }
                        }
                    }


                    adapter.setData(workPartBeanList);
                    adapter.notifyDataSetChanged();
                }
                break;
            // 编辑生产周期
            case ConstantUtil.START_ACTIVITY_KEY_2:
                if (data != null) {
                    rl_production_cycle.setVisibility(View.VISIBLE);
                    beginDate = data.getStringExtra(ConstantUtil.KEY_1);
                    completeDate = data.getStringExtra(ConstantUtil.KEY_2);
                    tv_production_cycle.setText(beginDate + "---" + completeDate);
                }
                break;
//            // 删除加工信息
//            case ConstantUtil.DELETE_WORKPART_INFOMATION:
//                if (data != null) {
//                    WorkPartBean wpBean= (WorkPartBean) data.getSerializableExtra(ConstantUtil.KEY_1);
//                    if (selectedPosition >= 0){
//                        workPartBeanList.remove(selectedPosition);
//                        adapter.setData(workPartBeanList);
//                        adapter.notifyDataSetChanged();
//                        selectedPosition = -1;
//                    }
//                }
//                break;
            // 确认编辑
            case ConstantUtil.START_ACTIVITY_KEY_3:
                if (null != data) {
                    WorkPartBean workPartBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    if (selectedPosition >= 0) {
                        workPartBeanList.remove(selectedPosition);
                        adapter.setData(workPartBeanList);
                        adapter.notifyDataSetChanged();
                        selectedPosition = -1;
                    } else {
                        for (int i = 0; i < produce_productBean.getWorkPartList().size(); i++) {
                            if (workPartBean.getWorkPartId().equals(produce_productBean.getWorkPartList().get(i).getWorkPartId())) {
                                produce_productBean.getWorkPartList().set(i, workPartBean);
                                adapter.setData(produce_productBean.getWorkPartList());
                                adapter.notifyDataSetChanged();
                                break;
                            }
                        }
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getOrderId() {
        return produce_productBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return produce_productBean.getProductId();
    }

    @Override
    public void getProductDetailSuccessed(Produce_ProductBean productBean) {
        if (null != productBean.getWorkPartList() && !productBean.getWorkPartList().isEmpty()) {
            rl_production_cycle.setVisibility(View.VISIBLE);
            tv_production_cycle.setText(DateUtil.getYyyy_Mm_Dd(productBean.getBeginDate()) + "---" + DateUtil.getYyyy_Mm_Dd(productBean.getFinishDate()));
            workPartBeanList.clear();
            workPartBeanList.addAll(productBean.getWorkPartList());
            adapter.setData(workPartBeanList);
            adapter.notifyDataSetChanged();
            lv_add_workpart.setAdapter(adapter);

            if (null != productBean.getWorkPartList()) {
                v_1.setVisibility(View.VISIBLE);
            } else {
                v_1.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void getProductDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
